import time

#导入浏览器包
from selenium.webdriver.remote.webdriver import WebDriver
#导入页面显示操作方法
from selenium.webdriver.remote.webelement import WebElement

#显示等待包
from selenium.webdriver.support.ui import WebDriverWait
#导入显示等待判断包
from selenium.webdriver.support import expected_conditions as e

from selenium.webdriver.support.select import Select
#设置默认时间
TIMEOUT = 10


class Base():
    def __init__(self, driver):
        self.driver = driver

    def open_url(self, url):
        self.driver.get(url)

    #显示等待点击

    def wait_click(self, loc, timeout=None) -> WebElement:
        timeout = TIMEOUT if timeout is None else timeout
        wait = WebDriverWait(self.driver, timeout=timeout)

        return wait.until(e.element_to_be_clickable(loc))

    #显示等待
    def wait_vaue(self, loc, timeout=None) -> WebElement:
        timeout = TIMEOUT if timeout is None else timeout
        wait = WebDriverWait(self.driver, timeout=timeout)

        return wait.until(e.visibility_of_element_located(loc))

    #封装点击点击方法
    def click(self, loc):
        e = self.wait_click(loc)
        e.click()

    #封装点击输入方法
    def send_keys(self, loc, info):
        e = self.wait_vaue(loc)
        e.send_keys(info)

    #封装断言方法
    def get_text(self, loc):
        return self.wait_click(loc).text

    #封装强制等待方法
    def sleep(self):
        time.sleep(10)

    #封装切换窗口
    def execute_script(self,loc):
        self.driver.execute_script(f'window.scrollTo(0,{loc})')

    def select(self,loc,x):
        day=Select(self.wait_click(loc))
        day.select_by_value(x)
    #下拉框选择
    def select2(self, loc, x):
        day = Select(self.wait_click(loc))
        day.select_by_index(x)
    #下拉框选择
    def select3(self, loc, x):
        day = Select(self.wait_click(loc))
        day.select_by_visible_text(x)

    # 切换页面
    def window(self,loc):
        self.driver.switch_to.window(self.driver.window_handles[-1])